// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
/**
 * 商店
 */
let base = require("panelBase")
let viewConfig = {
    btn_close:{
        node:null,
        name:"btn_close",
        eventType:"click",
        cb:"onBtnCloseClick"
    },
    item:{
        node:null,
        name:"item",
        active:false,
    },
    // btn_buy:{
    //     node:null,
    //     name:"item/shop_buy",
    //     eventType:"click",
    //     cb:"onBuyItemClick",
    // },
    shopSc:{
        node:null,
        name:"shopSc",
    }
};
cc.Class({
    extends: base,

    properties: {
        cakeList:[],
        mySc:null,
    },

    init () {
        this._super(viewConfig);
        this.onHide();
        this.node.addComponent("myScrollView");
        this.mySc = this.node.getComponent("myScrollView");
        this.mySc.init(viewConfig["shopSc"].node);
        this.cakeList = this.getCakeList();

   
    },

    onShow(){
        this._super();
        setTimeout(()=>{
            let testList = [];
            for(let i = 0 ; i < 10 ;++i)
            {
                let testData = {
                    cake_id:1,
                    cakeName:"法式小面包",
                    level:3,
                    count:0,
                    price:100,
                }
                testData.cake_id = i;
                testData.cakeName = i%2 == 0 ? "法式小面包":"蛋糕";
                testData.count = i;
                testData.price = 10+ i*20;
                testList.push(testData);
            }

            this.mySc.showData(viewConfig["item"].node,testList,this.initItem.bind(this),10);
        },1000)
     },


    initItem(node,data){
        if(!node || !data )
            return ;
        this.updateInfo(node.getChildByName("cake"),this.cakeList.getSpriteFrame(`cake_${data.cake_id}`),cc.Sprite);
        this.updateInfo(node.getChildByName("cakeName"),data.cakeName,cc.Label);
        this.updateInfo(node.getChildByName("level"),"等级:"+data.level,cc.Label);
        this.updateInfo(node.getChildByName("count"),"已经有"+data.count+"个");
        let price = cc.find("shop_buy/price",node)
        this.updateInfo(price,data.price,cc.Label);
    },

    onBtnCloseClick(){
        this.onHide();
    },


    onBuyItemClick(){
        //cc.log(arguments);
        let index = arguments[0].target.parent.index;
        cc.log(index);
    },


    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    }
});
